﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CodeTester
{
   
    public class Program
    {
        static void Main(string[] args)
        {
            //TreeBasicTest();
            //SingleLinkedListTest();
            //DoubleLinkedListTest();
            //BubbleSortTest();
            //FibonacciTest(); 
            //MatrixSearchTest(); 
            //ReverseTest();
            Console.WriteLine("main begin");
            TaskTestAsync();
            Console.WriteLine("main end");

            Console.ReadKey();
        }

        #region Test01:单向链接测试
        static void SingleLinkedListTest()
        {
            SingleLinkedList<int> linkedList = new SingleLinkedList<int>();
            // Test1:顺序插入4个节点
            linkedList.Add(0);
            linkedList.Add(1);
            linkedList.Add(2);
            linkedList.Add(3);

            Console.WriteLine("The nodes in the linkedList:");
            for (int i = 0; i < linkedList.Count; i++)
            {
                Console.WriteLine(linkedList[i]);
            }
            Console.WriteLine("----------------------------");

            // Test2.1:在索引为0(即第1个节点)的位置插入单个节点
            linkedList.Insert(0, 10);
            Console.WriteLine("After insert 10 in index of 0:");
            for (int i = 0; i < linkedList.Count; i++)
            {
                Console.WriteLine(linkedList[i]);
            }
            // Test2.2:在索引为2(即第3个节点)的位置插入单个节点
            linkedList.Insert(2, 20);
            Console.WriteLine("After insert 20 in index of 2:");
            for (int i = 0; i < linkedList.Count; i++)
            {
                Console.WriteLine(linkedList[i]);
            }
            // Test2.3:在索引为5（即最后一个节点）的位置插入单个节点
            linkedList.Insert(5, 30);
            Console.WriteLine("After insert 30 in index of 5:");
            for (int i = 0; i < linkedList.Count; i++)
            {
                Console.WriteLine(linkedList[i]);
            }
            Console.WriteLine("----------------------------");

            // Test3.1:移除索引为5（即最后一个节点）的节点
            linkedList.RemoveAt(5);
            Console.WriteLine("After remove an node in index of 5:");
            for (int i = 0; i < linkedList.Count; i++)
            {
                Console.WriteLine(linkedList[i]);
            }
            // Test3.2:移除索引为0（即第一个节点）的节点
            linkedList.RemoveAt(0);
            Console.WriteLine("After remove an node in index of 0:");
            for (int i = 0; i < linkedList.Count; i++)
            {
                Console.WriteLine(linkedList[i]);
            }
            // Test3.3:移除索引为2（即第三个节点）的节点
            linkedList.RemoveAt(2);
            Console.WriteLine("After remove an node in index of 2:");
            for (int i = 0; i < linkedList.Count; i++)
            {
                Console.WriteLine(linkedList[i]);
            }
            Console.WriteLine("----------------------------");
        }
        #endregion

        #region Test02:双向链接测试
        static void DoubleLinkedListTest()
        {
            DoubleLinkedList<int> linkedList = new DoubleLinkedList<int>();
            // Test1:顺序插入4个节点
            linkedList.AddAfter(0);
            linkedList.AddAfter(1);
            linkedList.AddAfter(2);
            linkedList.AddAfter(3);

            Console.WriteLine("The nodes in the DoubleLinkedList:");
            for (int i = 0; i < linkedList.Count; i++)
            {
                Console.Write(linkedList[i] + " ");
            }
            Console.WriteLine();
            Console.WriteLine("----------------------------");
            // Test2.1:在尾节点之前插入2个节点
            linkedList.AddBefore(10);
            linkedList.AddBefore(20);
            Console.WriteLine("Before add 10 and 20:");
            for (int i = 0; i < linkedList.Count; i++)
            {
                Console.Write(linkedList[i] + " ");
            }
            Console.WriteLine();
            // Test2.2:在索引为2(即第3个节点)的位置之后插入单个节点
            linkedList.InsertAfter(2, 50);
            Console.WriteLine("insertAfter  50 in index of 2");
            for (int i = 0; i < linkedList.Count; i++)
            {
                Console.Write(linkedList[i] + " ");
            }
            Console.WriteLine();
            // Test2.3:在索引为2(即第3个节点)的位置之前插入单个节点
            linkedList.InsertBefore(2, 40);
            Console.WriteLine("InsertBefore  40 in  index of 2");
            for (int i = 0; i < linkedList.Count; i++)
            {
                Console.Write(linkedList[i] + " ");
            }
            Console.WriteLine();
            Console.WriteLine("----------------------------");
            // Test3.1:移除索引为7(即最后一个节点)的位置的节点
            linkedList.RemoveAt(7);
            Console.WriteLine("remove an node in index of 7:");
            for (int i = 0; i < linkedList.Count; i++)
            {
                Console.Write(linkedList[i] + " ");
            }
            Console.WriteLine();
            // Test3.2:移除索引为0(即第一个节点)的位置的节点的值
            linkedList.RemoveAt(0);
            Console.WriteLine("remove an node in index of 0:");
            for (int i = 0; i < linkedList.Count; i++)
            {
                Console.Write(linkedList[i] + " ");
            }
            Console.WriteLine();
            // Test3.3:移除索引为2(即第3个节点)的位置的节点
            linkedList.RemoveAt(2);
            Console.WriteLine("remove an node in index of 2:");
            for (int i = 0; i < linkedList.Count; i++)
            {
                Console.Write(linkedList[i] + " ");
            }
            Console.WriteLine();
            Console.WriteLine("----------------------------");
            // Test4:修改索引为2(即第3个节点)的位置的节点的值
            linkedList[2] = 9;
            Console.WriteLine("update the value of node in index of 2:");
            for (int i = 0; i < linkedList.Count; i++)
            {
                Console.Write(linkedList[i] + " ");
            }
            Console.WriteLine();
            Console.WriteLine("----------------------------");
        }
        #endregion

        #region Test03:二叉树基本测试
        static void TreeBasicTest()
        {
            // 构造一颗二叉树，根节点为"A"
            BTree<string> bTree = new BTree<string>("A");
            TreeNode<string> rootNode = bTree.Root;
            // 向根节点"A"插入左孩子节点"B"和右孩子节点"C"
            bTree.InsertLeft(rootNode, "B");
            bTree.InsertRight(rootNode, "C");
            // 向节点"B"插入左孩子节点"D"和右孩子节点"E"
            TreeNode<string> nodeB = rootNode.LChild;
            bTree.InsertLeft(nodeB, "D");
            bTree.InsertRight(nodeB, "E");
            // 向节点"C"插入右孩子节点"F"
            TreeNode<string> nodeC = rootNode.RChild;
            bTree.InsertRight(nodeC, "F");

            // 前序遍历
            Console.WriteLine("---------前序遍历二叉树---------");
            bTree.PreOrder(bTree.Root);
            // 中序遍历
            Console.WriteLine();
            Console.WriteLine("---------中序遍历二叉树---------");
            bTree.MidOrder(bTree.Root);
            // 后序遍历
            Console.WriteLine();
            Console.WriteLine("---------后序遍历二叉树---------");
            bTree.PostOrder(bTree.Root);
        }
        #endregion

        #region Test04:冒泡排序算法测试
        static void BubbleSortTest()
        {
            
            Sorting sort = new Sorting();
            int[] a = { 5, 4, 6, 7, 1, 4, 0, 9, 12, 55, 34 };
            //string[] b = { "d", "e", "a", "f", "m", "t", "z", "ek", "bb" };
            a = sort.BubbleSort(a, (c, d) => { return c.CompareTo(d) > 0; });

            foreach (int i in a)
            {
                Console.Write(i);
                Console.Write(",");
            }
        }

        static bool Compare<T>(T a, T b) where T : IComparable
        {
            return a.CompareTo(b) > 0;                
        }

        #endregion

        #region Test05:斐波那契数列测试
        static void FibonacciTest()
        {
            FibonacciRecursively fib = new FibonacciRecursively();

            uint n = 45;

            long temp1 = fib.Fibonacci(n);//递归
            long temp2 = fib.FibonacciNew(n);//循环

            Console.Write("{0}、{1}",temp1,temp2);
        }
        #endregion

        #region Test06:有序的二维数组查询测试
        static void MatrixSearchTest()
        {
            int[,] matrix = { { 1, 2, 8, 9 }, { 2, 4, 9, 12 }, { 4, 7, 10, 13 }, { 6, 8, 11, 15 } };
            ArraySearch search = new ArraySearch();

            int searchValue = 13;
            bool isFind = search.Search(matrix, 4, 4, searchValue);

            Console.Write("{0}{1}二维数组中", searchValue, isFind ? "存在" : "不存在");

        }
        #endregion

        #region Test07:反转字符串测试
        static void ReverseTest()
        {
            string temp = "I am iaw!";
            StringReverse sr = new StringReverse();
            char[] result = sr.Reverse(temp.ToArray(), 0, temp.Length-1);

            Console.Write(new string(result));
        }
        #endregion

        #region Test08:Task使用测试
        static async Task TaskTestAsync()
        {
            Console.WriteLine("TaskTest Begin");
            TaskLearn t = new TaskLearn();
            long re=await t.TestAsync(40);
            Console.WriteLine("TaskTest end");

        }
        #endregion

       
    }



}
